package com.cloud.async.result.core.product;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;

/**
 * @author chengrui
 * @title: StandardResult
 * @projectName webCloudAsync
 * @description: 结果处理具体实现类（标准实现）
 * @date 2021/10/10 20:38
 */
public class StandardResult extends AsyncResult {

    @Override
    public String getResult(String jsonStr) {
        return jsonStr;
    }

    @Override
    public <T> T getResult(Class<T> requiredType, String jsonStr) {
        String result = getResult(jsonStr);
        T t = JSONObject.parseObject(result, requiredType);
        return t;
    }

    @Override
    public <T> T getResult(Class<T> requiredType, String jsonStr, String... args){
        String result = getResult(jsonStr);
        JSONObject retJsonObj = JSONObject.parseObject(result);
        if (ObjectUtil.isNotEmpty(args)) {
            for (String arg : args) {
                if (ObjectUtil.isEmpty(retJsonObj.getJSONObject(arg))) {
                    return null;
                }
                retJsonObj = retJsonObj.getJSONObject(arg);
            }
        } else {
            return getResult(requiredType, jsonStr);
        }
        return JSONObject.toJavaObject(retJsonObj,requiredType);
    }

}
